var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        curPage : "/zofui_sales/ucard/list",
        page : {
            isend : false,
            doing : false,
            doo : 'ucard',
            pdata : {
                op : 'prizelist',
                page : 1,
                isinit : 1,
                otype : 0,
                sortid : 0,
                paktype : 0,
                pakby : 0,
                pakstyle : 2,
                search : '',
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        sort : [],
		paktype : [],
        pakby : [],
        pakstyle : [],
    },
    onLoad:function(options){
        var self = this;
		var shopid = options.shopid ? options.shopid : '';
        
        var zfid = options.zfid ? options.zfid : '';
        var sortid = options.sid ? options.sid : 0;
        app.com.getLocation(self,function(loc){
            self.setData({
                'page.pdata.lat' : loc.latitude,
                'page.pdata.lng' : loc.longitude,
                'page.pdata.isinit' : 1,
                'page.pdata.page' : 1,
                options : options,
                'page.pdata.zfid' : zfid,
                'page.pdata.sortid' : sortid,
                'page.isend' : false,
                'page.doing' : false,
            });
            app.com.auth(function(user){
                app.com.getPage(self,self.data.page,0,function(res){
                    var data = res.data.data;

                    self.setData({
                        datalist : data.list,
                        params : data.params,
                        sharedata : data.sharedata,
                        paktype : data.paktype,
                        pakby : data.pakby,
                        pakstyle : data.pakstyle,
                        sort : data.sort ? data.sort : [],
                        inited : true,
                        'page.pdata.isinit' : 0,
                    });
                    
                    wx.setNavigationBarTitle({
                        title:  res.data.data.params.title ? res.data.data.params.title : '礼包',
                    });

                },false,function(res){
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(0);
                    });
                });
				app.com.setBar(app,self,shopid);
            });
        });
    },
    onShareAppMessage : function(e){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : '',
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.data.isdown = true;
        this.onLoad( this.data.options );
        this.data.isdown = false;

        wx.stopPullDownRefresh();
    },
    onChange(e){
        var self = this;
        if( self.data.ising ) return false;
        self.data.ising = true;

        self.setData({
            datalist : [],
            'page.pdata.page' : 1,
            'page.pdata.sortid' : e.detail.name,
            'page.isend' : false,
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    changepak(e){
        var self = this;
        var type = e.currentTarget.dataset.type*1;
        if(type == 1){
            this.data.page.pdata.paktype = e.detail;
        }else if(type == 2){

            this.data.page.pdata.pakby = e.detail;

        }else if(type == 3){
            this.setData({
                'page.pdata.pakstyle' : e.detail
            })
            return false;
        }

        self.setData({
            datalist : [],
            'page.pdata.page' : 1,
            'page.isend' : false,
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
    },
    searchfn(e){
        var self = this;
        if(!e) return false;
        self.setData({
            datalist : [],
            'page.pdata.page' : 1,
            'page.pdata.search' : e.detail,
            'page.isend' : false,
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
    },
    clearsearch(){
        var e = {
            detail : 0,
        };
        this.searchfn(e);
    },
    showselect(){
        this.setData({
            showselect : !this.data.showselect
        })
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }

})